package com.kws.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kws.common.core.domain.R;
import com.kws.system.domain.vo.CreateDictTypeRq;
import com.kws.system.domain.vo.QueryDictTypeRq;
import com.kws.system.domain.vo.UpdateDictTypeRq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.kws.common.core.utils.poi.ExcelUtil;
import com.kws.common.log.annotation.Log;
import com.kws.common.log.enums.BusinessType;
import com.kws.common.security.annotation.RequiresPermissions;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.system.api.domain.SysDictType;
import com.kws.system.service.ISysDictTypeService;

/**
 * 数据字典信息
 * 
 * @author zhangyh
 */
@RestController
@Api(tags = "字典管理")
@RequestMapping("/dict/type")
public class SysDictTypeController
{
    @Autowired
    private ISysDictTypeService dictTypeService;

//    @RequiresPermissions("system:dict:list")
    @PostMapping("/list")
    @ApiOperation("分页查询字典列表")
    public R<Page<SysDictType>> list(@RequestBody QueryDictTypeRq rq)
    {
        Page<SysDictType> pageInfo = dictTypeService.selectDictTypeList(rq);
        return R.ok(pageInfo);
    }

//    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("system:dict:export")
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, SysDictType dictType)
//    {
//        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
//        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
//        util.exportExcel(response, list, "字典类型");
//    }

    /**
     * 查询字典类型详细
     */
//    @RequiresPermissions("system:dict:query")
    @ApiOperation("根据字典类型ID查询详情")
    @GetMapping(value = "/{dictId}")
    public R getInfo(@PathVariable Long dictId)
    {
        return R.ok(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
//    @RequiresPermissions("system:dict:add")
    @ApiOperation("新增字典类型")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody CreateDictTypeRq rq)
    {
        return dictTypeService.createDictType(rq);
    }

    /**
     * 修改字典类型
     */
//    @RequiresPermissions("system:dict:edit")
    @ApiOperation("修改字典类型")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody UpdateDictTypeRq rq)
    {
        return dictTypeService.updateDictType(rq);
    }

    /**
     * 删除字典类型
     */
//    @RequiresPermissions("system:dict:remove")
    @ApiOperation("删除字典类型")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public R remove(@PathVariable Long[] dictIds)
    {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return R.ok();
    }

    /**
     * 刷新字典缓存
     */
//    @RequiresPermissions("system:dict:remove")
//    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
//    @DeleteMapping("/refreshCache")
//    public R refreshCache()
//    {
//        dictTypeService.resetDictCache();
//        return R.ok();
//    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    @ApiOperation("获取字典选择框列表")
    public R optionselect()
    {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return R.ok(dictTypes);
    }
}
